<?php
/**
 * media management 媒体管理，包含了对以下两个地址的管理APP_PATH和UPLOAD_PATH
 * 这两个地址可能会重复
 */
class pMediaAction extends pBaseAction{

    private $homes=array('APP'=>APP_PATH,'UPLOAD'=>UPLOAD_PATH);
    private $root;
    private $subdir;

    public function _initialize(){
        $this->setPath();
    }

    public function index(){
        $files=$this->getSeparateSubFiles($this->homes[$this->root].$this->subdir);
        $this->assign('folders',$files['folders']);
        $this->assign('files',$files['files']);
        $this->assign('rootName',L($this->root));
        $this->assign('root',$this->root);
        $this->assign('subdir',$this->subdir);
        $this->display();
    }

    public function removal(){/*{{{*/
        $filename=(array_key_exists('filename',$_POST))?$_POST['filename']:false;
        if($filename===false){
            $this->ajaxReturn('failure');
            return;
        }
        $file=$this->homes[$this->root].$this->subdir.$filename;
        $file=(IS_WIN)?auto_charset($file,'utf-8','gbk'):$file;
        if(is_file($file) && @unlink($file)){
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('failure');
        }
    }/*}}}*/

    /**
     * 接收上传的文件,fancyUpload
     **/
    public function save(){/*{{{*/
        noCache();
        $return=array();
        require THINK_PATH.'/Lib/ORG/Net/UploadFile.class.php';
        $uploader=new UploadFile();
        $uploader->saveRule='';
        $uploader->uploadReplace=true;
        $uploader->savePath=$this->homes[$this->root].$this->subdir;
        if($uploader->upload()){
            $item=array_shift($uploader->getUploadFileInfo());
            $return['status']='1';
            $return['name']=$item['savename'];
            $return['size']=getSuitableFileSize($item['size']);
            $return['mtime']=date('Y-m-d H:i:s',filemtime($item['savepath']));
            $return['ext']=$item['extension'];
        }else{
            $return['status']='0';
            $return['error']=$uploader->getErrorMsg();
        }
        echo json_encode($return);
    }/*}}}*/

    private function setPath(){/*{{{*/
        //先获取相对的根目录
        $root=$this->getSid('root');
        if(false!==$root && array_key_exists($root,$this->homes) ){
            $this->root=$root;
        }else{
            $this->root='APP';
        }
        //再获取要浏览的子目录
        $subdir=$this->getSid('subdir');
        $this->subdir=(false===$subdir)?'/':rtrim($subdir,'\\/').'/';
    }/*}}}*/

    /*
     * 返回传入目录下的所有文件，目录在前，文件在后
     */
    private function getSeparateSubFiles($path){/*{{{*/
        $files=new DirectoryIterator($path);
        $ret_folder=array();
        $ret_file=array();
        foreach($files as $file){
            $a=array('isFile'=>true,'fileName'=>convertFileName($file->getFilename()),'lastModified'=>date('Y-m-d H:i:s',$file->getMTime()));
            if($file->isFile()){
                $a['size']=$file->getSize();
                $a['ext']=substr(strrchr($a['fileName'],'.'),1);
                $ret_file[]=$a;
            }else if($file->isDir()){
                $a['isfile']=false;
                $ret_folder[]=$a;
            }
        }
        return array('folders'=>$ret_folder,'files'=>$ret_file);
    }/*}}}*/

    private function getSubFiles($path){
        $ret=$this->getSeparateSubFiles($path);
        return array_merge($ret['foldesr'],$ret['files']);
    }
}
